﻿<!--#Include File = "CustomTemplate.asp" -->

<%
	Call  PageController.RenderPage()
	Public Function Page_Configure()
		PageController.PageTitle = "Category management"
		PageController.AuthorizedRoles =  Array("admin")
		pageController.RequiresAuthentication  = True
		PageController.PageCSSFiles = "main.css" 	
		
	End function
%>
<%Public Function Page_RenderForm()%>
	<H2>Category management</H2>
	
	<%	Page.Execute %>
	<%txtQuery%> <%cmdSearch%>
	<br/>
	<Span Class="Caption">List of Category<HR></Span>	
	<%'tvTree.Render%>
	<%gdCategory%>	
	
	<hr>
	<a href="CategoryEdit.asp?parentid=<%Response.Write(parentID)%>">Add new</a>
<%End Function%>




<%  'This would normaly go in a another page, but for the sake of simplicity and to minimize the number of pages
    'I'm including code behind stuff here...	
	Dim gdCategory
	Dim parentID
	Dim tvTree
	
	Dim txtQuery
	Dim cmdSearch
	Public Function Page_Load()
	
		If Not Page.IsPostBack Then
			If Request.QueryString("parentid")<>"" Then
				parentID = Clng(Request.QueryString("parentid"))			
			Else
				parentID =1
			End if
			Call ShowRecords()
		End If	
	End Function
	Public Function Page_Init()
		Set tvTree	 = New_ServerTreeView("tvTree","")
		'Page.DebugEnabled = True
		Set gdCategory = New_ServerDataGrid("gdCategory")		
		Page.AutoResetScrollPosition = True
		
		Set txtQuery = New_ServerTextBoxEX("txtQuery",100,40)
		Set cmdSearch = New_ServerButton("cmdSearch")	
	
	End Function

	Public Function Page_Controls_Init()
		cmdSearch.Text = "Search"
		
		
		'For category tree
		Dim sSQL
		Dim rs
		tvTree.AddRoot "Root","",""
		sSQL = "SELECT id,name,parentid From category where parentid=1 Order By 2"
		Set rs = GetRecordSet(sSQL)
		While Not rs.EOF
			With tvTree.Nodes(0).AddNode(rs(1).Value,"","")
				.SetAttribute "ID",rs(0).Value
				.SetAttribute "X","R"		
			End With
			rs.MoveNext
		Wend
	
	End Function
	
	Public Function cmdSearch_OnClick()
		
	
	End Function
	
	Public Function Page_LoadViewState()
		
		parentID = Page.ViewState.GetValue("ID") 'Get from viewstate	
	End Function

	
	Public Function Page_PreRender()	
		Page.ViewState.Add "ID",parentID 'Add to viewstate :-)	
		Call ShowRecords()
	End Function
	
Public Sub SetTemplate(obj)		 
	obj.AlternatingItemStyle = "background-color:#dddddd;font-size:8pt;border:1px solid black" 
	obj.ItemStyle = "font-size:8pt;border:1px solid black"
	obj.Control.Style = "width:100%;border-collapse:collapse;"
	obj.HeaderStyle = "font-weight:bold;color:white;background-color:#4682b4;font-size:10pt;font-family:tahoma;border:1px solid black"
	obj.BorderWidth = 1
	obj.BorderColor = "Black"
	'obj.GenerateColumns() 'Just so it generates most of them :-)
	obj.CreateColumns Array("id" , "name","description","") , Array("CategoryID", "CategoryName","Description","Action")
	
	obj.AutoGenerateColumns = False  'Now lets not have it generate the columns again during render!
	obj.Columns(1).ColumnType = 2    'Modify the column settings we need.
	obj.Columns(1).DataValueField = "id"
	obj.Columns(1).DataTextField = "name"
	obj.Columns(1).DataFormatString = "<A HREF='Category.asp?parentid={1}' Class='ActionLinkNormal'>{0}</A>"

	obj.Columns(3).ColumnType = 3    'Modify the column settings we need.
	obj.Columns(3).HeaderText = "Actions"
	obj.Columns(3).HorizonalAlign = "center"
	obj.Columns(3).CellRenderFunctionName = "RenderActionColumn"
	
	
	obj.Pager.ProgressStyle = "font-size:8pt"
	obj.Pager.PrevNextStyle = "font-size:8pt"
	obj.Pager.PagerStyle    = "font-size:8pt"	
	obj.AllowPaging = True
	obj.Pager.PagerType = 0
	obj.Pager.PagerSize = 20
	
	
End Sub

Public Function RenderActionColumn(ds)
		Response.Write "<A HREF='CategoryEdit.asp?parentid=" & ds("parentid").Value & "&id=" & ds("id").Value & "' Class='ActionLink'>edit</A> | "
		Response.Write "<A " & Page.GetEventScript("HREF","Page","DeleteCategory",ds("id").Value,"") & " Class='ActionLink'>delete</a>"		
End Function

Public Function Page_DeleteCategory(e)
		Dim sSQL
		Dim rs
		'delete all children
		sSQL = "SELECT * from category where parentid=" & Replace(e.Instance,"'","''")
		Set rs = DBLayer.GetRecordSet(sSQL)		
		If rs.RecordCount <> 0 Then
			
			rs("isdeleted").Value	= true	
	
			DBLayer.UpdateRecordSet rs
		End If		
		
		
		
		'delete parent
		sSQL = "SELECT * FROM category Where id=" & Replace(e.Instance,"'","''")
		
		Set rs = DBLayer.GetRecordSet(sSQL)		
		If rs.RecordCount <> 0 Then
			
			rs("isdeleted").Value	= true	
	
			DBLayer.UpdateRecordSet rs
		End If		
		
		
	
End Function

Public Function ShowRecords()
	Dim sSQL
	if txtQuery.Text = "" Then
		sSQL = "Select c.id, c.name, c.description, c.parentid From category as c  Where c.isDeleted = 0 And c.parentid = " & parentID 
	else
		sSQL = "Select c.id, c.name, c.description,c.parentid From category as c  Where c.isDeleted = 0 And c.name like N'%"& txtQuery.Text & "%' or c.description like N'%"&txtQuery.Text & "%'" 
	end if
	'Response.Write(sSQL)
	Set gdCategory.DataSource = GetRecordset(sSQL)

	SetTemplate gdCategory

End Function


'WEBCONTROLS POSTBACK EVENT HANDLERS
	Public Function tvTree_OnClick(node) 
		If node.GetAttribute("P") <> "" Then
			Exit Function
		End If
		node.SetAttribute "P","1"
		node.IsOpened = True
		Select Case node.GetAttribute("X")
			Case "R": 
				LoadOrders node
			Case "O": 
				LoadOrdersDetails node				
			Case Else
				Exit Function
		End Select	
	End Function
	
	Function LoadOrders(node)
		Dim sSQL
		Dim rs
		
		sSQL = "SELECT id,name FROM category Where parentid = " & Replace(node.GetAttribute("ID"),"'","''") & ""
		Response.Write("Load node" & sSQL)
		Set rs = GetRecordSet(sSQL)
		While Not rs.EOF
			With node.AddNode(rs(1).Value,"","")
				.SetAttribute "ID",rs(0).Value
				.SetAttribute "X","O"
			End With
			rs.MoveNext
		Wend
	End Function

	Function LoadOrdersDetails(node)
		Dim sSQL
		Dim rs
		sSQL = "SELECT id,name FROM category Where parentid = '" & Replace(node.GetAttribute("ID"),"'","''") & "'"
		Response.Write("Load detail" & sSQL)
		Set rs = GetRecordSet(sSQL)
		While Not rs.EOF
			With node.AddNode(rs(1).Value,"","")
				.SetAttribute "ID",rs(0).Value
				.SetAttribute "X","O"
			End With
			rs.MoveNext
		Wend	
	End Function


		
	
%>
